<template>
  <div class="app-container">
    <basic-container v-show="showSearch">
      <common-search @handleQuery="handleQuery" @resetQuery="resetQuery">
        <el-form
          :model="queryParams"
          ref="queryForm"
          :inline="true"
          size="mini"
          label-width="98px"
        >
          <el-form-item label="运输批次号" prop="manifestId">
            <el-input
              v-model.trim="queryParams.manifestId"
              placeholder="请输入运输批次号"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="关区代码" prop="customsCode">
            <el-select
              v-model="queryParams.customsCode"
              placeholder="请选择关区代码"
              filterable
              clearable
            >
              <el-option
                v-for="(item, index) in customsList"
                :key="index"
                :label="item.customsCode + '-' + item.customsName"
                :value="item.customsCode"
              ></el-option>
            </el-select>
          </el-form-item>
<!--          <el-form-item label="起始监管场所" prop="startFieldCode">
            <el-select
              v-model="queryParams.startFieldCode"
              placeholder="请选择起始监管场所"
              @change="handleQuery"
              filterable
              clearable
            >
              <el-option
                v-for="(item, index) in fieldOptions"
                :key="index"
                :label="item.label"
                :value="item.value"
              ></el-option>
            </el-select>
          </el-form-item>-->
          <el-form-item label="申报状态" prop="status">
            <el-select
              v-model.trim="queryParams.status"
              placeholder="请选择申报状态"
              clearable
            >
              <el-option
                v-for="(item, index) in dict.type.mft_bd_decl_status"
                :key="index"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <!-- <el-form-item label="装载时间" prop="loadTime">
            <el-date-picker
              clearable
              v-model.trim="queryParams.loadTime"
              type="date"
              value-format="yyyy-MM-dd HH:mm:ss"
              placeholder="选择装载时间不能为空"
            >
            </el-date-picker>
          </el-form-item> -->
          <el-form-item label="卸货点代码" prop="unloadCode">
            <el-input
              v-model.trim="queryParams.unloadCode"
              placeholder="请输入卸货点代码"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="装货地代码" prop="loadCode">
            <el-input
              v-model.trim="queryParams.loadCode"
              placeholder="请输入装货地代码"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <!-- <el-form-item label="承运人代码" prop="carrierCode">
            <el-input
              v-model.trim="queryParams.carrierCode"
              placeholder="请输入承运人代码"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item> -->
          <!-- <el-form-item label="确报类型" prop="bindingtype">
            <el-select
              v-model.trim="queryParams.bindingtype"
              placeholder="请选择确报类型"
              clearable
            >
              <el-option
                v-for="(item, index) in dict.type.binding_types"
                :key="index"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item> -->
        </el-form>
      </common-search>
    </basic-container>

    <basic-container>
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="danger"
            plain
            icon="el-icon-delete"
            size="mini"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['bt:mftManifestHead:remove']"
          >删除
          </el-button
          >
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-upload"
            size="mini"
            @click="handleDecl"
            v-hasPermi="['bt:mftManifestHead:decl']"
          >申报
          </el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-upload"
            size="mini"
            @click="handleUpdateDecl"
            v-hasPermi="['bt:mftManifestHead:mftUpdateDecl']"
          >变更
          </el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="danger"
            plain
            icon="el-icon-upload"
            size="mini"
            @click="handleDeleteDecl"
            v-hasPermi="['bt:mftManifestHead:mftDelDecl']"
          >撤销
          </el-button>
        </el-col>
        <el-col :span="1.5">
          <!-- 查看回执 -->
          <view-response :disabled="single" :msgType="mType" :checkedId="manifestIdList"></view-response>
        </el-col>
        <!-- <el-col :span="1.5">
          <el-button
            type="success"
            plain
            icon="el-icon-upload"
            size="mini"
            @click="handleDeclQB"
            v-hasPermi="['bt:mftManifestHead:bdDecl']"
          >确报新增申报
          </el-button>
        </el-col> -->
        <!-- <el-col :span="1.5">
          <el-button
            type="warning"
            plain
            icon="el-icon-download"
            size="mini"
            :loading="exportLoading"
            @click="handleExport"
            v-hasPermi="['bt:mftManifestHead:export']"
          >导出</el-button>
        </el-col>
        -->
        <right-toolbar
          :showSearch.sync="showSearch"
          @queryTable="getList"
        ></right-toolbar>
      </el-row>

      <!-- 列表 -->
      <basic-table
        ref="tableList"
        :columns="columns"
        :tableData="tableData"
        :total="pageParams.total"
        @handleSizeChange="handleSizeChange"
        @handleCurrentChange="handleCurrentChange"
        :loading="loading"
        :needCheckbox="true"
        @selectionChange="handleSelectionChange"
      >
        <!-- 关区 -->
        <template #customsCode="scope">
          {{ scope.row.customsCode }}-
          <dict-value
            :list="customsList"
            value="customsCode"
            label="customsName"
            :dictCode="scope.row.customsCode"
          ></dict-value>
        </template>
        <!-- 场所 -->
        <template #startFieldCode="scope">
          <dict-value
            :list="fieldOptions"
            :dictCode="scope.row.startFieldCode"
          ></dict-value>
        </template>
        <!-- 确报类型 -->
        <template #bindingType="scope">
          <dict-tag
            :options="dict.type.binding_types"
            :value="scope.row.bindingType"
          ></dict-tag>
        </template>
        <template #declStatus="scope">
          <dict-tag
            :options="dict.type.mft_decl_status"
            :value="scope.row.declStatus"
          ></dict-tag>
        </template>


        <template #rcptStatus="scope">
          <dict-tag
            :options="dict.type.mft_bd_decl_status"
            :value="scope.row.rcptStatus"
          ></dict-tag>
        </template>
        <template #iEFlag="scope">
          <dict-tag
            :options="dict.type.sys_i_e_flag"
            :value="scope.row.iEFlag"
          ></dict-tag>
        </template>
        <template #currCode="scope">
          <dict-value
            :list="bt_currency"
            :dictCode="scope.row.currCode"
          ></dict-value>
        </template>
        <template #customsStateCode="scope">
          <dict-value
            :list="dict.type.mft_biz_mode"
            :dictCode="scope.row.customsStateCode"
          ></dict-value>
        </template>
        <template #wrapType="scope">
          <dict-value
            :list="dict.type.custom_wrap_type"
            :dictCode="scope.row.wrapType"
          ></dict-value>
        </template>
        <template #operate="scope">
          <div class="option-menu">
            <el-button
              type="text"
              size="mini"
              class="table-edit"
              @click="handleDetail(scope.row)"
            >
              详情
            </el-button>
          </div>
        </template>
      </basic-table>
    </basic-container>



    <!-- 新增、编辑 -->
    <add-edit
      ref="addEdit"
      :dialogType="dialogType"
      :rowData="rowData"
      :dict="dictProp"
      :customsList="customsList"
      :fieldOptions="fieldOptions"
      @handleQuery="handleQuery"
    />

    <!-- 详情 -->
    <detail-dialog
      ref="detail"
      :dict="dictProp"
      :customsList="customsList"
      :fieldOptions="fieldOptions"
      :detailId="detailId"
    ></detail-dialog>

    <!-- 撤销原因 -->
    <el-dialog
      title="是否确认撤销该申报？"
      :visible.sync="dialogVisibleDecl"
      width="500px">
      <el-form :inline="true" ref="formInline" :model="formInline" :rules="formInlineRules" class="demo-form-inline" size="small">
        <el-form-item label="撤销原因：" style="width: 100%;" prop="reasonCode">
          <el-select v-model="formInline.reasonCode" placeholder="请选择撤销原因"  style="width: 300px;">
            <el-option v-for="item in dict.type.mft_revoke_reason" :key="item.value" :label="item.label" :value="item.value"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="cancelDeclDialog">取 消</el-button>
        <el-button type="primary" @click="deleteDeclSubmit">确 定</el-button>
      </span>
    </el-dialog>

  </div>
</template>

<script>
import {
  listMftManifest,
  delMftManifestHead,
  exportMftManifestHead,
  declMftManifestHead,
  declQBMftManifestHead,
  updateMftManifestDecl,
  deleteMftManifestDecl,
  deleteQBMftManifestDecl
} from "@/api/swdecl/mftManifestHead";
import addEdit from "./components/addEdit";
import detailDialog from "./components/detailDialog";
import {findListCustoms} from "@/api/system/customs";

import {findBaseFieldInfo, findZcField} from "@/api/system/baseFieldInfo";
import ViewResponse from '../components/viewResponse.vue';
import curr_type from "@/assets/dicts/curr_type.json"

export default {
  name: "MftManifestHead",
  components: {
    addEdit,
    detailDialog,
    ViewResponse
  },
  dicts: ["binding_types", "mft_bd_decl_status", "sys_i_e_flag", "mft_biz_mode", "custom_wrap_type", "mft_revoke_reason",'mft_decl_status'],
  data() {
    return {
      // 显示搜索条件
      showSearch: true,
      mType: 'MT_14_24',
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        manifestId: null,
        iEFlag: null,
        customsStateCode: null,
        agentEntCode: null,
        customsCode: null,
        loadTime: null,
        unloadCode: null,
        loadCode: null,
        customMasterCode: null,
        arrivalDate: null,
        status: null,
        msgRepCode: null,
        msgRepName: null,
        startFieldCode: null,
        startFieldName: null,
        carrierCode: null,
        bindingtype: null,
      },
      // 遮罩层
      loading: true,
      // 列表数据
      tableData: [],
      columns: [
        {
          prop: "id",
          label: "统一申报编号",
          align: "center",
          minWidth: 160,
        },
        {
          prop: "manifestId",
          label: "运输批次号",
          align: "center",
          minWidth: 140,
        },
        {
          prop: "billNo",
          label: "提运单号",
          align: "center",
          minWidth: 140,
        },
        {
          prop: "iEFlag",
          label: "进出口",
          align: "center",
          minWidth: 100,
          slot: true,
        },
        {
          prop: "customsStateCode",
          label: "业务类型",
          align: "center",
          minWidth: 100,
          slot: true
        },
        {
          prop: "packNo",
          label: "数量",
          align: "center",
          minWidth: 130,
        },
        {
          prop: "grossWt",
          label: "毛重(KG)",
          align: "center",
          minWidth: 100
        },
        {
          prop: "goodsPrice",
          label: "货值",
          align: "center",
          minWidth: 100
        },
        {
          prop: "currCode",
          label: "货币",
          align: "center",
          slot: true,
          minWidth: 140
        },
        {
          prop: "customsCode",
          label: "关区代码",
          align: "center",
          minWidth: 140,
          slot: true,
        },
        {
          prop: "msgRepName",
          label: "申报企业",
          align: "center",
          minWidth: 140
        },
        {
          prop: "createTime",
          label: "创建时间",
          align: "center",
          width: 140,
        },
        {
          prop: "createBy",
          label: "创建人",
          align: "center",
          width: 140,
        },
        {
          prop: "declStatus",
          label: "申报状态",
          align: "center",
          slot: true,
          minWidth: 120,
          fixed: "right"
        },
        {
          prop: "rcptStatus",
          label: "回执状态",
          align: "center",
          slot: true,
          minWidth: 120,
          fixed: "right"
        },
        {
          prop: "rcptRemark",
          label: "回执说明",
          align: "center",
          minWidth: 120,
          fixed: "right"
        },
        {
          prop: "operate",
          label: "操作",
          slot: true,
          width: 90,
          fixed: "right",
        },
      ],
      pageParams: {
        total: 0,
      },
      rowData: {},
      detailId: "",
      dialogType: "",
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      //详情显示的时候，表单编辑禁用
      isViewDisabled: false,
      // 舱单申报头表格数据
      mftManifestHeadList: [],
      //海关关区信息列表
      customsList: [],
      // 场所列表
      fieldOptions: [],
      // 选中的批次号
      manifestIdList: [],
      bt_currency: curr_type.value, //货币类型字典
      dialogVisibleDecl: false,
      formInline: {
        reasonCode: ""
      },
      formInlineRules: {
        reasonCode: [{
          required: true,
          message: '撤销原因不能为空',
          trigger: 'blur'
        },],
      }
    };
  },
  computed: {
    dictProp() {
      //字典list，为避免子组件重复调接口获取，用computed计算传值
      return {
        type: this.dict.type,
      };
    },
  },
  watch: {
    $route: {
      handler: function(route) {
        this.queryParams.iEFlag = route.query && route.query.iEFlag;
      },
      immediate: true
    }
  },
  created() {
    this.getList();
    this.loadCustomsList();
    this.getLoadBaseFieldInfoList();
  },
  methods: {
    /** 查询舱单申报头列表 */
    getList() {
      this.loading = true;
      listMftManifest(this.queryParams).then((response) => {
        this.tableData = response.rows;
        this.pageParams.total = response.total;
        this.loading = false;
      });
    },
    /* 加载关区信息列表*/
    loadCustomsList() {
      findListCustoms().then((response) => {
        this.customsList = response.data;
      });
    },
    //获取场所下拉列表
    getLoadBaseFieldInfoList() {
      findBaseFieldInfo({
        customsCode: this.userCustomsCode
      }).then(response => {
        this.fieldOptions = response.data;
        /*if (this.selectBaseFieldInfoList != null && this.selectBaseFieldInfoList.length == 1) {
          this.form.workFieldCode = this.selectBaseFieldInfoList[0].value;
        }
        if (this.selectBaseFieldInfoList.length > 0) {
          this.selectGateBaseFieldInfoList = this.selectBaseFieldInfoList.filter(n => "2" == n.extVal1);
        }*/

      })
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.$refs.tableList.toPageFirst();
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 分页
    handleSizeChange(val) {
      this.queryParams.pageSize = val.pageSize;
      this.getList();
    },
    handleCurrentChange(val) {
      this.queryParams.pageNum = val.currentPage;
      this.getList();
    },
    // 新增/编辑
    handleAddEdit(type, row) {
      this.dialogType = type;
      if (row) {
        this.rowData = JSON.parse(JSON.stringify(row));
      }
      this.$refs.addEdit.open(type, row);
    },
    // 详情
    handleDetail(row) {
      this.detailId = row.id;
      this.$nextTick(() => {
        this.$refs.detail.open();
      });
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.manifestIdList = selection.map((item) => item.manifestId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除舱单申报头编号为"' + ids + '"的数据项？')
        .then(function () {
          return delMftManifestHead(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {
        });
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$modal
        .confirm("是否确认导出所有舱单申报头数据项？")
        .then(() => {
          this.exportLoading = true;
          return exportMftManifestHead(queryParams);
        })
        .then((response) => {
          this.download(response.msg);
          this.exportLoading = false;
        })
        .catch(() => {
        });
    },
    //舱单申报
    handleDecl(row) {
      const billCodes = row.id || this.ids;
      if (billCodes == undefined || billCodes == null || billCodes == "") {
        this.$modal.alertError("请先选择要舱单申报的单证数据！");
        return false;
      }
      this.$modal.confirm('是否确认申报业务编号为"' + billCodes + '"的数据项？').then(function () {
        return declMftManifestHead(billCodes);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("申报成功");
      }).catch(() => {
      });
    },
    // 变更申报
    handleUpdateDecl(row) {
      const billCodes = row.id || this.ids;
      if (billCodes == undefined || billCodes == null || billCodes == "") {
        this.$modal.alertError("请先选择要确保申报的单证数据！");
        return false;
      }
      this.$modal.confirm('是否确认变更业务编号为"' + billCodes + '"的申报数据项？').then(function () {
        return updateMftManifestDecl(billCodes);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("申报成功");
      }).catch(() => {
      });
    },
    cancelDeclDialog() {
      this.dialogVisibleDecl = false;
      this.formInline = {
        reasonCode: ""
      }
    },
    // 删除申报
    handleDeleteDecl(row) {
      this.dialogVisibleDecl = true;
    },
    deleteDeclSubmit() {
      this.$refs["formInline"].validate(valid => {
        if (valid) {
          const params = {
            id: this.ids[0],
            reasonCode: this.formInline.reasonCode
          }
          deleteMftManifestDecl(params).then(res => {
            this.cancelDeclDialog();
            this.$modal.msgSuccess("申报成功");
            this.getList();
          })
        }
      })
    }
  },
};
</script>


<style lang="scss" scoped>
.el-form-item .el-select {
  width: 100%;
}
</style>
